package com.team.game.Screens;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import android.view.MotionEvent;

import com.team.game.Assets;
import com.team.game.Game.Gestures;
import com.team.game.framework.Game;
import com.team.game.framework.Level;
import com.team.game.framework.Screen;
import com.team.game.framework.impl.SurfaceThread;
import com.team.game.framework.impl.MainActivity;
import com.team.game.framework.impl.Scale_X_Y;

import java.io.IOException;
import java.io.InputStream;

import static com.team.game.framework.impl.Scale_X_Y.Scale_X;
import static com.team.game.framework.impl.Scale_X_Y.Scale_Y;
import static com.team.game.framework.impl.Scale_X_Y.Size_X;
import static com.team.game.framework.impl.Scale_X_Y.Size_Y;
import static com.team.game.framework.impl.Scale_X_Y.scale;

public class Main_Screen extends Screen {

    @SuppressWarnings("unused")
    private static final String TAG = "Main_Screen";

    private final RectF rectPlay;
    private RectF rectOptions;

    private RectF rectBackground;
    private Bitmap background, play, setting;

    public Main_Screen (Game game) {
        //Вызов родительского метода
        super (game);
        SurfaceThread.wait = true;


        InputStream inputStream;
        try {
            inputStream = MainActivity.manager.open("Picture/background.jpg");
            background = BitmapFactory.decodeStream (inputStream);
            inputStream = MainActivity.manager.open("Picture/play.png");
            play = Bitmap.createScaledBitmap (BitmapFactory.decodeStream (inputStream), (int) (680 * Scale_X), (int) (300 * Scale_Y), false);
            inputStream = MainActivity.manager.open("Picture/options.png");
            setting = Bitmap.createScaledBitmap (BitmapFactory.decodeStream (inputStream), (int) (680 * Scale_X), (int) (300 * Scale_Y), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //вычисляем размеры
        rectBackground = new RectF (0, 0, Scale_X_Y.Size_X, Size_Y);
        rectPlay = new RectF (200 * Scale_X, 300 * Scale_Y, 880 * Scale_X, 600 * Scale_Y);
        rectOptions = new RectF (200 * Scale_X, 700 * Scale_Y, 880 * Scale_X, 1000 * Scale_Y);

        SurfaceThread.wait = false;
    }

    @Override
    public boolean touchEvent (MotionEvent motionEvent) {

        if (motionEvent.getAction () == MotionEvent.ACTION_UP && !SurfaceThread.wait) {

            //Settings
            if (Assets.inRect (rectOptions, motionEvent))
                game.setScreen (new Setting_Screen (game));
            else if (Assets.inRect (rectPlay, motionEvent))
                game.setScreen (new Maps_Screen (game));
            else {
                rectOptions.set (200 * Scale_X, 700 * Scale_Y, 880 * Scale_X, 1000 * Scale_Y);
                rectPlay.set (200 * Scale_X, 300 * Scale_Y, 880 * Scale_X, 600 * Scale_Y);
            }
        }
        if (motionEvent.getAction () == MotionEvent.ACTION_DOWN && !SurfaceThread.wait) {
            //Settings
            if (Assets.inRect (rectOptions, motionEvent))
                rectOptions.set (rectOptions.left + 20 * scale, rectOptions.top + 20 * scale,
                        rectOptions.right - 20 * scale, rectOptions.bottom - 20 * scale);
            //Locations
            if (Assets.inRect (rectPlay, motionEvent))
                rectPlay.set (rectPlay.left + 20 * scale, rectPlay.top + 20 * scale,
                        rectPlay.right - 20 * scale, rectPlay.bottom - 20 * scale);
        }
        return true;
    }

    @Override
    public void update () {
    }

    long time;
    float t;
    @Override
    public void present (Canvas canvas) {

        //time = System.nanoTime ();

        //Фон
        canvas.drawBitmap (background, null, rectBackground, null);
        //Кнопка Play
        canvas.drawBitmap (play, null, rectPlay, null);
        //Кнопка Options
        canvas.drawBitmap (setting, null, rectOptions, null);

        //t = (System.nanoTime () - time) / 1000000000.0f;
        //Log.i ("Main_scr present: ", "" + t);

    }

    @Override
    public void pause () {
    }

    @Override
    public void resume () {
    }

    @Override
    public void dispose () {
        setting.recycle();
        background.recycle();
        play.recycle();

        rectBackground = null;
        rectOptions = null;
        rectBackground = null;

    }

    @Override
    public void back() {
    }

    @Override
    public Level getLevel() {
        return null;
    }

    @Override
    public Gestures getGestures() {
        return null;
    }
}